
#include "datparser.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <time.h>

static int restore_data_file(void)
{
	int ret = 0;
	char buf[1024] = {0};

	FILE *psrc = NULL;
	FILE *pdest = NULL;
	
	pdest = fopen("/etc/Wireless/RT2870AP/RT2870AP.dat", "w+");
	if(NULL != pdest)
	{
		psrc = fopen("/etc/Wireless/RT2870AP/RT2870AP.dat.bak", "r");
		if(NULL != psrc)
		{
			ret = 1024;
			while(1024 <= ret)
			{
				ret = fread(buf, 1, sizeof(buf), psrc);
				fwrite(buf, 1, ret, pdest);

				fflush(psrc);
				fflush(pdest);
			}
			fclose(psrc);
			fclose(pdest);
			return 0;
		}
		fclose(pdest);
	}
	return -1;
}

int main(int argc, char **argv)
{
	struct dat_stu *list = NULL;
	
	char ssid[32] = "AJB_1234564214";
	char password[32] = "359571153627";

	restore_data_file();
	if(0 == dat_load("/etc/Wireless/RT2870AP/RT2870AP.dat", &list))
	{
		if(NULL != list)
		{
			if(0 == dat_set_lable(list, "SSID", ssid, strlen(ssid)) &&  0 == dat_set_lable(list, "WPAPSK", password, strlen(password)))
			{
				//dat_dump(list);
				dat_save("/etc/Wireless/RT2870AP/RT2870AP.dat", list);
			}
			dat_free(&list);
			return 0;
		}
	}
	printf("failed\n");
	return 0;
}